/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prim;

import java.util.ArrayList;

public class Tree {

    public ArrayList<Node> nodes;
    static Tree t;
    static ArrayList<Node> visitedNodes = new ArrayList<Node>();
    ArrayList<Edge> possilbeEdges = new ArrayList<Edge>();


    static int cost = 0;

    public Tree() {
        nodes = new ArrayList<Node>();

    }

    public static void main(String[] args) {
        // TODO code application logic here
        t = new Tree();
        t.addNode("A", "B", "C", "D", "E", "F", "G");
        t.setDistance("A", "B", 7);
        t.setDistance("A", "D", 5);
        t.setDistance("B", "C", 8);
        t.setDistance("D", "B", 9);
        t.setDistance("A", "D", 5);
        t.setDistance("C", "E", 5);
        t.setDistance("B", "E", 7);
        t.setDistance("D", "E", 15);
        t.setDistance("D", "F", 6);
        t.setDistance("F", "E", 8);
        t.setDistance("E", "G", 9);
        t.setDistance("F", "G", 11);
        visitedNodes.add(t.findNode("A"));
        t.findPath();
        System.out.println("Total cost: " + cost);

    }

    public void addNode(String... args) {
        for (String i : args) {
            addNode(i);
        }
    }

    public void addNode(String nodeName) {
        Node n = new Node(nodeName);
        if (!nodes.contains(n)) {
            nodes.add(n);
        }
    }

    public void setDistance(String A, String B, int distance) {
        Node a, b;
        if (!A.equals(B)) {
            a = findNode(A);
            b = findNode(B);
            if (a != null && b != null) {
                a.addEdge(new Edge(a, b, distance));
                b.addEdge(new Edge(b, a, distance));
            }
        }
    }

    public Node findNode(String key) {
        for (Node n : nodes) {
            if (n.name.equalsIgnoreCase(key)) {
                return n;
            }
        }
        return null;
    }

    public void findshortes(String from, String to) {
        ArrayList<String> visited = new ArrayList<String>();

    }

    public void findPath() {
        Node currentNode = null;
        Edge bestEdge = null;
        while (visitedNodes.size() != t.nodes.size()) {
            for (Node tn : t.nodes) {
                for (Edge te : tn.routes) {
                    if (!possilbeEdges.contains(te)) {
                        for (Node n : visitedNodes) {
                            if (te.parent.equals(n) || te.target.equals(n)) {
                                if (bestEdge == null || te.distance < bestEdge.distance) {
                                    bestEdge = te;
                                    currentNode = n;
                                }
                            }
                        }
                        if (!bestEdge.parent.equals(currentNode)) {
                            visitedNodes.add(bestEdge.parent);
                        } else if (!bestEdge.target.equals(currentNode)) {
                            visitedNodes.add(bestEdge.target);
                        }
                    }
                }
            }



            possilbeEdges.add(bestEdge);


        }
        for (Edge e : possilbeEdges) {
            cost += e.distance;
        }


    }
}